package com.sq.subject.application.controller;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson2.JSON;
import com.google.common.base.Preconditions;
import com.sq.subject.application.converter.SubjectCategoryDTOConverter;
import com.sq.subject.application.dto.*;
import com.sq.subject.common.context.LoginContextHolder;
import com.sq.subject.common.entity.Result;
import com.sq.subject.common.util.LoginUtil;
import com.sq.subject.domain.bo.CategoryAndLabelBO;
import com.sq.subject.domain.bo.PrimaryCategoryBO;
import com.sq.subject.domain.bo.SubjectCategoryBO;
import com.sq.subject.domain.service.SubjectCategoryDomainService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author quan
 */
@RestController
@RequestMapping("/subject/category")
@Slf4j
public class SubjectCategoryController {
    @Resource
    private SubjectCategoryDomainService subjectCategoryDomainService;

    @PostMapping("/add")
    public Result<Boolean> addSubjectCategory(@RequestBody SubjectCategoryDTO subjectCategoryDTO) {
        String loginId = LoginUtil.getLoginId();
        log.info("SubjectCategoryController.addSubjectCategory.loginId:{}",loginId);
        if (log.isInfoEnabled()) {
            log.info("SubjectCategoryController.addSubjectCategory.dto：{}", JSON.toJSONString(subjectCategoryDTO));
        }
        try {
            Preconditions.checkNotNull(subjectCategoryDTO.getCategoryType(), "分类类型不能为空");
            Preconditions.checkNotNull(subjectCategoryDTO.getParentId(), "分类父级不能为空");
            Preconditions.checkArgument(!StringUtils.isEmpty(subjectCategoryDTO.getCategoryName()), "分类名称不能为空");
            SubjectCategoryBO subjectCategoryBO = SubjectCategoryDTOConverter.INSTANCE.convertDTOToSubjectCategoryBO(subjectCategoryDTO);
            subjectCategoryDomainService.add(subjectCategoryBO);
            return Result.ok();
        } catch (Exception e) {
            log.error("SubjectCategoryController.addSubjectCategory.error:{}", e.getMessage(), e);
            return Result.fail("新增分类失败");
        }
    }

    @PostMapping("/update")
    public Result<Boolean> updateSubjectCategory(@RequestBody SubjectCategoryDTO subjectCategoryDTO) {
        if (log.isInfoEnabled()) {
            log.info("SubjectCategoryController.updateSubjectCategory.dto：{}", JSON.toJSONString(subjectCategoryDTO));
        }
        try {
            Preconditions.checkNotNull(subjectCategoryDTO.getId(), "id不能为空");
            SubjectCategoryBO subjectCategoryBO = BeanUtil.copyProperties(subjectCategoryDTO, SubjectCategoryBO.class);
            subjectCategoryDomainService.update(subjectCategoryBO);
            return Result.ok();
        } catch (Exception e) {
            log.error("SubjectCategoryController.updateSubjectCategory.error:{}", e.getMessage(), e);
            return Result.fail("更新分类失败");
        }
    }

    @GetMapping("/queryPrimaryCategory")
    public Result<List<PrimaryCategoryDTO>> querySubjectPrimaryCategoryCategory() {
        try {
            List<PrimaryCategoryBO> primaryCategoryBOList = subjectCategoryDomainService.querySubjectPrimaryCategoryCategory();
            List<PrimaryCategoryDTO> primaryCategoryDTOList = BeanUtil.copyToList(primaryCategoryBOList, PrimaryCategoryDTO.class);
            return Result.ok(primaryCategoryDTOList);
        } catch (Exception e) {
            log.error("SubjectCategoryController.queryPrimaryCategory.error:{}", e.getMessage(), e);
            return Result.fail("查询主分类失败");
        }
    }

    @PostMapping("/queryCategoryByPrimary")
    public Result<List<SubjectCategoryDTO>> queryCategoryByPrimary(@RequestBody CategoryByPrimaryDTO categoryByPrimary) {
        if (log.isInfoEnabled()) {
            log.info("SubjectCategoryController.queryCategoryByPrimary.dto：{}", JSON.toJSONString(categoryByPrimary));
        }
        try {
            SubjectCategoryBO subjectCategoryBO = BeanUtil.copyProperties(categoryByPrimary, SubjectCategoryBO.class);
            List<SubjectCategoryBO> categoryBOList = subjectCategoryDomainService.queryCategoryByPrimary(subjectCategoryBO);
            List<SubjectCategoryDTO> categoryDTOList = BeanUtil.copyToList(categoryBOList, SubjectCategoryDTO.class);
            return Result.ok(categoryDTOList);
        } catch (Exception e) {
            log.error("SubjectCategoryController.queryCategoryByPrimary.error:{}", e.getMessage(), e);
            return Result.fail("由主分类id查分类失败");
        }
    }

    @PostMapping("/queryCategoryAndLabel")
    public Result<List<CategoryAndLabelDTO>> queryCategoryAndLabel(@RequestBody SubjectCategoryDTO subjectCategoryDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectCategoryController.queryCategoryAndLabel.dto：{}", JSON.toJSONString(subjectCategoryDTO));
            }
            SubjectCategoryBO subjectCategoryBO = BeanUtil.copyProperties(subjectCategoryDTO, SubjectCategoryBO.class);
            List<CategoryAndLabelBO> categoryAndLabelList = subjectCategoryDomainService.queryCategoryAndLabel(subjectCategoryBO);
            List<CategoryAndLabelDTO> categoryAndLabelDTOList = BeanUtil.copyToList(categoryAndLabelList, CategoryAndLabelDTO.class);
            for (int i = 0; i < categoryAndLabelDTOList.size(); i++) {
                categoryAndLabelDTOList.get(i).setLabelDTOList(BeanUtil.copyToList(categoryAndLabelList.get(i).getLabelBOList(), SubjectLabelDTO.class));
            }
            return Result.ok(categoryAndLabelDTOList);
        } catch (Exception e) {
            log.error("SubjectCategoryController.queryCategoryAndLabel.error:{}", e.getMessage(), e);
            return Result.fail("查询主分类及其下的标签集合失败");
        }
    }

    @PostMapping("/delete")
    public Result<Boolean> delete(@RequestBody SubjectCategoryDTO subjectCategoryDTO){
        if (log.isInfoEnabled()) {
            log.info("SubjectCategoryController.delete.dto：{}", JSON.toJSONString(subjectCategoryDTO));
        }
        Preconditions.checkNotNull(subjectCategoryDTO.getId(), "id不能为空");
        try{
            SubjectCategoryBO subjectCategoryBO = BeanUtil.copyProperties(subjectCategoryDTO, SubjectCategoryBO.class);
            subjectCategoryDomainService.delete(subjectCategoryBO);
            return Result.ok();
        }catch (Exception e){
            log.error("SubjectCategoryController.queryCategoryAndLabel.error:{}", e.getMessage(), e);
            return Result.fail("删除分类失败");
        }

    }
}
